package top.jolyoulu.jlwechatpub.service.impl;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jolyoulu.jlwechatpub.config.VxParameterConfig;
import top.jolyoulu.jlwechatpub.entity.dto.AccessTokenDTO;
import top.jolyoulu.jlwechatpub.entity.dto.CodeAccessTokenDTO;
import top.jolyoulu.jlwechatpub.entity.dto.CodeUserInfoDTO;
import top.jolyoulu.jlwechatpub.service.AccessTokenService;
import top.jolyoulu.jlwechatpub.wechatpub.utils.HttpUtil;
import top.jolyoulu.webcommon.enums.GlobalExpType;
import top.jolyoulu.webcommon.excption.GlobalException;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @Author: LZJ
 * @Date: 2020/5/25 20:27
 * @Version 1.0
 */
@Slf4j
@Service
@Transactional
public class AccessTokenServiceImpl implements AccessTokenService {

    @Autowired
    private VxParameterConfig vxParameterConfig;


    @Override
    public AccessTokenDTO getAccessToken() {
        String url = vxParameterConfig.getAccessTokenUrl();
        String appId = vxParameterConfig.getAppId();
        String appSecret = vxParameterConfig.getAppSecret();
        url = url.replaceAll("APPID",appId);
        url = url.replaceAll("SECRET",appSecret);
        String jsonString = HttpUtil.sendGet(url);
        JSONObject parseObject = JSONObject.parseObject(jsonString);
        if (Objects.nonNull(parseObject.getInstant("errcode"))){
            log.error("请求微信服务器失败 ==> {}",jsonString);
            throw new GlobalException(GlobalExpType.WEACHT_LOGIN_ERROR);
        }
        return JSONObject.parseObject(jsonString, AccessTokenDTO.class);
    }

    @Override
    public CodeAccessTokenDTO getAccessTokenByCode(String code) {
        String url = vxParameterConfig.getCodeAccessTokenUrl();
        String appId = vxParameterConfig.getAppId();
        String appSecret = vxParameterConfig.getAppSecret();
        url = url.replaceAll("APPID",appId);
        url = url.replaceAll("SECRET",appSecret);
        url = url.replaceAll("CODE",code);
        String jsonString = HttpUtil.sendGet(url);
        JSONObject parseObject = JSONObject.parseObject(jsonString);
        if (Objects.nonNull(parseObject.getInstant("errcode"))){
            log.error("请求微信服务器失败 ==> {}",jsonString);
            throw new GlobalException(GlobalExpType.WEACHT_LOGIN_ERROR);
        }
        return JSONObject.parseObject(jsonString, CodeAccessTokenDTO.class);
    }

    @Override
    public CodeUserInfoDTO getUserInfoByCodeAccessToken(String codeAccessToken, String openId) {
        String url = vxParameterConfig.getUserInfoByCodeAccessTokenUrl();
        url = url.replaceAll("ACCESS_TOKEN",codeAccessToken);
        url = url.replaceAll("OPENID",openId);
        String jsonString = HttpUtil.sendGet(url);
        JSONObject parseObject = JSONObject.parseObject(jsonString);
        if (Objects.nonNull(parseObject.getInstant("errcode"))){
            log.error("请求微信服务器失败 ==> {}",jsonString);
            throw new GlobalException(GlobalExpType.WEACHT_LOGIN_ERROR);
        }
        //微信给的是ISO_8859_1转码UTF_8
        jsonString = new String(jsonString.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return JSONObject.parseObject(jsonString, CodeUserInfoDTO.class);
    }
}
